//
//  AuctionPubNotisVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/5.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "AuctionPubNotisVC.h"
#import "AuctionPubNotisTableViewCell.h"
#import "XMJSWebViewController.h"

#pragma mark -- mark 数据相关
#import "AuctionMsgPublicNotisModel.h"

@interface AuctionPubNotisVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@end
static NSString *const identifier = @"auction_cell_identifier";
@implementation AuctionPubNotisVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"官方公告";
    self.view.backgroundColor = SKColorWithHex(0xF7F7F7);
    [self initContentViews];
}

- (void)initContentViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(SKTopBar_Height);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
}

#pragma mark -- mark 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = self.view.backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = [UIView new];
        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.showsHorizontalScrollIndicator = false;
        [_tableView registerClass:[AuctionPubNotisTableViewCell class] forCellReuseIdentifier:identifier];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


#pragma mark -- mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AuctionPubNotisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    AuctionMsgPublicNotisInfoModel *info = self.publicNotisData[indexPath.section];
    SKWeakSelf;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:info.img] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }];
    cell.title = info.title;
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.publicNotisData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    AuctionMsgPublicNotisInfoModel *info = self.publicNotisData[section];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[info.time integerValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:SS";
    NSString *dateString = [dateFormatter stringFromDate:date];
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, self.tableView.width, 54.0);
    UILabel *label = [[UILabel alloc] init];
    label.center = view.center;
    label.bounds = CGRectMake(0, 0, 130, 20);
    label.text = dateString ? dateString : @" ";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = SKFONT_SIZE(11);
    label.textColor = [UIColor whiteColor];
    label.layer.masksToBounds = true;
    label.layer.cornerRadius = 10;
    label.backgroundColor = SKColorWithHex(0x333333);
    label.layer.opacity = 0.4;
    [view addSubview:label];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 54.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AuctionMsgPublicNotisInfoModel *info = self.publicNotisData[indexPath.section];
    XMJSWebViewController * webVC = [[XMJSWebViewController alloc]init];
    webVC.type = WebType_H5_content;
    webVC.titleString = info.title;
    webVC.fileUrl = info.html;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
